const express = require('express')
const router = express.Router()
const db = require('../utils/db')

//删除文章
router.post('/admin/article/delete', (req, res) => {
    console.log(req.body)
    const { id } = req.body
    db(`UPDATE articles SET isDelete=1 WHERE id=${id}`, (err, data) => {
        // console.log(data)
        if (err || data.changedRows === 0) return res.send({ code: 400, msg: 'ID参数又问题' })
        res.send({ code: 200, msg: '删除成功' })
    })
})


//根据id查询指定文章
router.get('/admin/article/search/:id', (req, res) => {
    const { id } = req.params
    console.log(id)
        //  console.log(id)
    db(`SELECT id,title,cover,categoryId,date,content FROM articles WHERE id=${id} AND isDelete =0`, (err, data) => {
        console.log(data)
        if (err || data.length === 0) return res.send({ code: 400, msg: '查询失败，未获取到ID值' })
            //  console.log(data)
        res.send({ code: 200, msg: '获取成功', data: data[0] })

    })
})



//文章搜索
router.get('/admin/article/query', (req, res) => {
    let { key, type, state, page, perpage } = req.query
        // console.log({ key, type, state })
    if (!key) key = '.'
    if (!type) type = '.+'
    if (!state) state = '.'
    if (!page) page = 1
    if (!perpage) perpage = 6
    db(`SELECT articles.id,title,cover,date,content,state,author,'read',categories.name FROM articles LEFT JOIN categories ON articles.categoryId = categories.id WHERE title REGEXP '${key}' AND categoryId REGEXP '^${type}$' AND state REGEXP '${state}' AND isDelete =0 ORDER BY id DESC LIMIT ${page*perpage-perpage},${perpage}`, (err, data) => {
        if (err || data.length === 0) return res.send({ code: 400, msg: '查询失败，未获取到ID值' })
        db(`SELECT COUNT(*) totalCount FROM articles WHERE title REGEXP '${key}' AND categoryId REGEXP '${type}' AND state REGEXP '${state}' AND isDelete = 0`, (err, arr) => {
            const { totalCount } = arr[0]
            if (err || data.length === 0) return res.send({ code: 400, msg: '查询失败，未获取到ID值' })
            res.send({ totalCount: totalCount, totalPage: Math.ceil(totalCount / perpage), data: data })
        })
    })
})





//搜索文章接口
// router.get('/admin/article/query', (req, res) => {
//     let { key, type, state, page, perpage } = req.query
//         // console.log({ key, type, state })
//     if (!key) key = '.'
//     if (!type) type = '.'
//     if (!state) state = '.'
//     if (!page) page = 1
//     if (!perpage) perpage = 6
//     db(`SELECT articles.id,title,cover,date,content,state,author,'read',categories.name FROM articles LEFT JOIN categories ON articles.categoryId=categories.id WHERE title REGEXP '${key}' AND categoryId REGEXP '${type}' AND state REGEXP '${state}' AND isDelete = 0 ORDER BY id DESC LIMIT ${page*perpage-perpage},${perpage}`, (err, data) => {
//         if (err || data.length === 0) return res.send({ code: 400, msg: '搜索失败,没有符合条件的文章' })
//         db(`SELECT COUNT(*) totalCount FROM articles WHERE title REGEXP '${key}' AND categoryId REGEXP '${type}' AND state REGEXP '${state}' AND isDelete = 0`, (error, arr) => {
//             if (error || data.length === 0) return res.send({ code: 400, msg: '搜索失败,没有符合条件的文章' })
//             let { totalCount } = arr[0]
//             console.log({ totalCount })
//             res.send({ totalCount: totalCount, totalPage: Math.ceil(totalCount / perpage), data: data })
//         })
//     })
// })



module.exports = router